package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"gitee.com/rw555/fdsmastergo/internal/bluetooth"
	"gitee.com/rw555/fdsmastergo/pkg/logger"
	"gitee.com/rw555/fdsmastergo/pkg/version"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	// 初始化日志
	if err := logger.Configure(logger.Config{
		EnableConsole:       true,
		EnableCaller:        false,
		LogDirByServiceName: "bluetooth_server",
		MaxSize:             1,
		MaxBackups:          3,
		MaxAge:              3,
		SystemLogLevel:      zapcore.InfoLevel,
		OperationLogLevel:   zapcore.InfoLevel,
		NetworkLogLevel:     zapcore.InfoLevel,
		OtherLogLevel:       zapcore.DebugLevel,
	}); err != nil {
		fmt.Printf("初始化日志失败: %v\n", err)
		os.Exit(1)
	}
	defer logger.Sync()

	// 输出版本信息
	vInfo := version.NewInfo("bluetooth_server")
	logger.SystemInfo("蓝牙服务启动",
		zap.String("version", vInfo.Version),
		zap.String("gitCommit", vInfo.GitCommit),
		zap.String("buildTime", vInfo.BuildTime),
		zap.String("platform", vInfo.Platform))

	// 创建蓝牙服务
	server, err := bluetooth.NewServer()
	if err != nil {
		logger.SystemError("创建蓝牙服务失败", zap.Error(err))
	}

	// 启动服务
	if err := server.Start(); err != nil {
		logger.SystemError("启动蓝牙服务失败", zap.Error(err))
	}

	// 等待中断信号
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
	<-sigChan

	// 停止服务
	server.Stop()
	logger.SystemInfo("蓝牙服务已停止")
}
